﻿using System;
using System.Diagnostics.CodeAnalysis;
using PETimer;
using System.Threading.Tasks;
using System.Threading;

namespace ExampleServer {
    class PETimerExample {
        static void Main(string[] args) {

            //TickTimerExample2();
            //AsyncTimerExample();
            FrameTimerExample();

            Console.ReadKey();
        }

        static void FrameTimerExample() {
            FrameTimer timer = new FrameTimer(100);

            Task.Run(async () => {
                await Task.Delay(2000);
                timer.AddTask(
                    10,
                    (int tid) => {
                        WriteLine("tid:{0} work.", tid);
                    },
                    (int tid) => {
                        WriteLine("tid:{0} cancel.", tid);
                    },
                    5);

                while(true) {
                    timer.UpdateCheck();
                    Thread.Sleep(66);
                }
            });

            while(true) {
                string ipt = Console.ReadLine();
                if(ipt == "del") {
                    timer.DeleteTask(1);
                }
            }
        }

        static void AsyncTimerExample() {
            AsyncTimer timer = new AsyncTimer(true);
            timer.LogFunc = s => Console.WriteLine(s);
            timer.WarnFunc = s => Console.WriteLine(s);
            timer.ErrorFunc = s => Console.WriteLine(s);

            uint interval = 1000;
            int count = -1;
            int sum = 0;
            Task.Run(async () => {
                await Task.Delay(2000);
                DateTime historyTime = DateTime.UtcNow;
                timer.AddTask(
                    interval,
                    (int tid) => {
                        DateTime nowTime = DateTime.UtcNow;
                        TimeSpan ts = nowTime - historyTime;
                        historyTime = nowTime;
                        int delta = (int)(ts.TotalMilliseconds - interval);
                        sum += Math.Abs(delta);
                        WriteLine($"间隔差：{delta}");
                        WriteLine("tid:{0} work.", tid);
                    },
                    (int tid) => {
                        WriteLine("tid:{0} cancel.", tid);
                    },
                    count);
            });

            Task.Run(() => {
                while(true) {
                    timer.HandleTask();
                    Thread.Sleep(100);
                }
            });

            while(true) {
                string ipt = Console.ReadLine();
                if(ipt == "d") {
                    timer.DeleteTask(1);
                }
                else if(ipt == "calc") {
                    WriteLine("平均间隔：" + sum * 1.0F / count);
                }
            }
        }

        //1.独立线程检测时间（驱动），驱动线程执行回调
        static void TickTimerExample1() 
        {
            TickTimer timer = new TickTimer(10, false) ;
            timer.LogFunc = s => Console.WriteLine(s);
            timer.WarnFunc = s => Console.WriteLine(s);
            timer.ErrorFunc = s => Console.WriteLine(s);
            
            uint intervel = 66;
            int count = 50;
            int sum = 0;
            Task.Run(async () => {
                await Task.Delay(2000);
                DateTime historyTime = DateTime.UtcNow;
                timer.AddTask(intervel,
                    (int tid) => {
                        DateTime nowTime = DateTime.UtcNow;
                        TimeSpan ts = nowTime - historyTime;
                        historyTime = nowTime;
                        int delta = (int)(ts.TotalMilliseconds - intervel);
                        WriteLine($"间隔差：{delta}");
                        sum += Math.Abs(delta);
                        WriteLine("tid:{0} work.", tid);
                    },
                    (int tid) => {
                        WriteLine("tid:{0} cancel.", tid);
                    }, count);
            });

            while(true) {
                string ipt = Console.ReadLine();
                if(ipt == "del") {
                    timer.DeleteTask(1);
                }
                else if(ipt == "calc") {
                    WriteLine("平均间隔：" + sum * 1.0F / count);
                }
            }
        }
        //2.独立线程检测时间（驱动），外部线程执行回调
        static void TickTimerExample2() {
            TickTimer timer = new TickTimer(10, true);

            uint intervel = 66;
            int count = 50;
            int sum = 0;
            Task.Run(async () => {
                await Task.Delay(2000);
                DateTime historyTime = DateTime.UtcNow;
                timer.AddTask(intervel,
                    (int tid) => {
                        DateTime nowTime = DateTime.UtcNow;
                        TimeSpan ts = nowTime - historyTime;
                        historyTime = nowTime;
                        int delta = (int)(ts.TotalMilliseconds - intervel);
                        WriteLine($"间隔差：{delta}");

                        sum += Math.Abs(delta);
                        WriteLine("tid:{0} work.", tid);
                    },
                    (int tid) => {
                        WriteLine("tid:{0} cancel.", tid);
                    }, count);
            });

            Task.Run(async () => {
                while(true) {
                    //外部线程回调
                    timer.HandleTask();
                    await Task.Delay(10);
                }
            });

            while(true) {
                string ipt = Console.ReadLine();
                if(ipt == "del") {
                    timer.DeleteTask(1);
                }
                else if(ipt == "calc") {
                    WriteLine( "平均间隔：" + sum * 1.0F / count);
                }
            }

        }
        //3.外部线程检测时间（驱动），此外部线程执行回调
        static void TickTimerExample3() {
            TickTimer timer = new TickTimer(0, true);

            uint intervel = 66;
            int count = 50;
            int sum = 0;
            Task.Run(async () => {
                await Task.Delay(2000);
                DateTime historyTime = DateTime.UtcNow;
                timer.AddTask(intervel,
                    (int tid) => {
                        DateTime nowTime = DateTime.UtcNow;
                        TimeSpan ts = nowTime - historyTime;
                        historyTime = nowTime;
                        int delta = (int)(ts.TotalMilliseconds - intervel);
                        WriteLine($"间隔差：{delta}");
                        sum += Math.Abs(delta);
                        WriteLine("tid:{0} work.", tid);
                    },
                    (int tid) => {
                        WriteLine("tid:{0} cancel.", tid);
                    }, 
                    count);
            });

            Task.Run(async () => {
                while(true) {
                    //外部线程驱动 检测时间
                    timer.UpdateCheck();
                    //驱动线程回调
                    timer.HandleTask();
                    await Task.Delay(10);
                }
            });

            while(true) {
                string ipt = Console.ReadLine();
                if(ipt == "del") {
                    timer.DeleteTask(1);
                }
                else if(ipt == "calc") {
                    WriteLine("平均间隔：" + sum * 1.0F / count);
                }
            }

        }
        //4.外部线程检测时间（驱动），当前外部线程执行回调 与3其实相同 只是3多了一个handle
        static void TickTimerExample4() {
            TickTimer timer = new TickTimer(0, false);

            uint intervel = 66;
            int count = 50;
            int sum = 0;
            Task.Run(async () => {
                await Task.Delay(2000);
                DateTime historyTime = DateTime.UtcNow;
                timer.AddTask(intervel,
                    (int tid) => {
                        DateTime nowTime = DateTime.UtcNow;
                        TimeSpan ts = nowTime - historyTime;
                        historyTime = nowTime;
                        int delta = (int)(ts.TotalMilliseconds - intervel);
                        WriteLine($"间隔差：{delta}");
                        sum += Math.Abs(delta);
                        WriteLine("tid:{0} work.", tid);
                    },
                    (int tid) => {
                        WriteLine("tid:{0} cancel.", tid);
                    }, 
                    count);
            });

            Task.Run(async () => {
                while(true) {
                    //外部线程驱动
                    timer.UpdateCheck();
                    await Task.Delay(10);
                }
            });

            Task.Run(async () => {
                while(true) {
                    //外部线程回调
                    timer.HandleTask();
                    await Task.Delay(2);
                }
            });

            while(true) {
                string ipt = Console.ReadLine();
                if(ipt == "del") {
                    timer.DeleteTask(1);
                }
                else if(ipt == "calc") {
                    WriteLine("平均间隔：" + sum * 1.0F / count);
                }
            }
        }

        static void WriteLine(string line)
        {
            Console.Write($"Thread:{Thread.CurrentThread.ManagedThreadId}::");
            Console.WriteLine(line);
        }
        
        static void WriteLine([NotNull]string format,object arg1)
        {
            Console.Write($"Thread:{Thread.CurrentThread.ManagedThreadId}::");
            Console.WriteLine(format,arg1);
        }
    }

    
}
